// tabsView模块
import storage from '@/utils/storage.js'
import { TABS_ROUTES } from '@/store/mutationTypes.js'

// 不需要出现在标签页中的路由
const whiteList = ['redirect', 'login']

const state = {
  tabsList: []
}

const mutations = {
  // 初始化标签页
  initTabs(state, routes) {
    state.tabsList = routes
  },
  // 添加标签页
  addTabs(state, route) {
    if (whiteList.includes(route.name)) return false
    const isExists = state.tabsList.some(item => item.fullPath === route.fullPath)
    if (!isExists) {
      state.tabsList.push(route)
    }
    return true
  },
  // 关闭左侧
  closeLeftTabs(state, route) {
    const index = state.tabsList.findIndex(item => item.fullPath === route.fullPath)
    state.tabsList.splice(0, index)
  },
  // 关闭右侧
  closeRightTabs(state, route) {
    const index = state.tabsList.findIndex(item => item.fullPath === route.fullPath)
    state.tabsList.splice(index + 1)
  },
  // 关闭其他
  closeOtherTabs(state, route) {
    state.tabsList = state.tabsList.filter(item => item.fullPath === route.fullPath)
  },
  // 关闭当前页
  closeCurrentTab(state, route) {
    const index = state.tabsList.findIndex(item => item.fullPath === route.fullPath)
    state.tabsList.splice(index, 1)
  },
  // 关闭所有
  closeAllTabs(state) {
    state.tabsList = []
    storage.remove(TABS_ROUTES)
  }

}

const getters = {
  tabsList: (state) => state.tabsList
}

export default {
  namespaced: true,
  state,
  mutations,
  getters
}
